package com.jianyun.jyzs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.base.MVPBaseActivity;
import com.jianyun.jyzs.bean.MeUserInfo;
import com.jianyun.jyzs.presenter.MyProfilePresenter;
import com.jianyun.jyzs.utils.Const;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.PhotoUtils;
import com.jianyun.jyzs.utils.UploadPortraitUtil;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.view.iview.IMyProFileView;
import com.jianyun.jyzs.widget.ListItemTextView;
import com.jianyun.jyzs.widget.LoadingDialog;


import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的个人信息
 * Created by yangshijun on 2018/5/28.
 */

public class SetMyProfileActivity extends MVPBaseActivity<IMyProFileView, MyProfilePresenter> implements IMyProFileView {
    @BindView(R.id.tv_personal_portrait)
    TextView tvPersonalPortrait;
    @BindView(R.id.img_personal_portrait)
    ImageView imgPersonalPortrait;
    @BindView(R.id.iv_arrow)
    ImageView ivArrow;
    @BindView(R.id.ll_set_user_info)
    LinearLayout llSetUserInfo;
    @BindView(R.id.liv_nickname)
    ListItemTextView livNickname;
    @BindView(R.id.liv_enterprise_name)
    ListItemTextView livEnterpriseName;

    @BindView(R.id.liv_name)
    ListItemTextView livName;

    @BindView(R.id.liv_cellphone)
    ListItemTextView livCellphone;

    @BindView(R.id.liv_phone)
    ListItemTextView livPhone;

    @BindView(R.id.liv_email)
    ListItemTextView livEmail;

    @BindView(R.id.liv_department)
    ListItemTextView livDepartment;

    @BindView(R.id.liv_post)
    ListItemTextView livPost;

    @BindView(R.id.liv_superior)
    ListItemTextView livSuperior;

    public static final String UPLOAD_URI = "UPLOAD_URI";
    private ImageView personalPortrait;
    private UploadPortraitUtil uploadPortrait;
    private String portraitUrl;
    private String userId;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rce_activity_set_personal_profile);
        ButterKnife.bind(this);
        MeUserInfo userInfo = (MeUserInfo) getIntent().getSerializableExtra("userinfo");
        userId = LoginCache.getInstance().getUserInfo().getUserId();
        loadingDialog = LoadingDialog.create(this);
        if (userInfo != null) {
            //头像
            //收到
            String iconPaths = LoginCache.getInstance().getUserDetaileInfo().getIconPaths();
            Log.i("test","iconPaths:"+iconPaths);
            if (iconPaths.length()>25){
                String erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl();
                String url=erpRootUrl+"SCSEXEC/BrowseImage.aspx?width=180&height=180&"+iconPaths;
                Log.i("test","路径："+url);
                Glide.with(this).load(url).into(imgPersonalPortrait);
            }
            //昵称
            livNickname.setDetail(userInfo.getUserName());
            livNickname.setArrowVisibility(View.INVISIBLE);
            //公司
            livEnterpriseName.setTitle(userInfo.getCompany());
            livEnterpriseName.setArrowVisibility(View.INVISIBLE);
            //姓名
            livName.setDetail(userInfo.getName());
            //手机
            livCellphone.setDetail(userInfo.getPhoneNum());
            //邮箱
            livEmail.setDetail(userInfo.getEmail());
            //部门
            livDepartment.setDetail(userInfo.getDepartment());
            livDepartment.setArrowVisibility(View.INVISIBLE);
        }
        initUploadPortrait();
    }

    private void initUploadPortrait() {
        //修改头像
        uploadPortrait = new UploadPortraitUtil(SetMyProfileActivity.this, loadingDialog);

        findViewById(R.id.ll_set_user_info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadPortrait.showPhotoDialog();
            }
        });
        uploadPortrait.setUploadPortraitListener(new UploadPortraitUtil.onUploadPortraitListener() {
            @Override
            public void onUploadSuccess(final String thumbUrl, final String largeUrl) {
                Log.i("test", "照片：" + thumbUrl + "\n" + largeUrl);
            }

            @Override
            public void onUploadFailed(String msg) {
                Log.i("test","照片："+msg);
            }
        });
    }

    private void refreshUserInfo(String portraitUrl) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case PhotoUtils.INTENT_CROP:
                if (resultCode == RESULT_OK) {
                    loadingDialog.show();
                }
                uploadPortrait.getPhotoUtils().onActivityResult(SetMyProfileActivity.this, requestCode, resultCode, data);
                break;
            case PhotoUtils.INTENT_TAKE:
            case PhotoUtils.INTENT_SELECT:
                uploadPortrait.getPhotoUtils().onActivityResult(SetMyProfileActivity.this, requestCode, resultCode, data);
                break;
        }
    }

    public void setupFileSuccess(boolean isUploadFile) {

       //收到
        String iconPaths = LoginCache.getInstance().getUserDetaileInfo().getIconPaths();
        String erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl();
        String url=erpRootUrl+"SCSEXEC/BrowseImage.aspx?width=180&height=180&"+iconPaths;
        Glide.with(this).load(url).into(imgPersonalPortrait);
    }

    @NonNull
    @Override
    public MyProfilePresenter createPresenter() {
        return new MyProfilePresenter();
    }

    @Override
    public void onCreateActionBar(ActionBar actionBar) {
        final View actionBarView = actionBar.setActionBar(R.layout.rce_actionbar_option_text);
        ImageButton backImgBtn = (ImageButton) actionBarView.findViewById(R.id.imgbtn_custom_nav_back);
        backImgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBackResult();
                finish();
            }
        });
        TextView titleTextView = (TextView) actionBarView.findViewById(R.id.tv_custom_nav_title);
        titleTextView.setText(R.string.rce_setting_personal_info);
    }

    @Override
    public void onBackPressed() {
        setBackResult();
        super.onBackPressed();
    }

    private void setBackResult() {
        if (!TextUtils.isEmpty(portraitUrl)) {
            Intent intent = new Intent();
            intent.putExtra(Const.PORTRAIT, portraitUrl);
            setResult(RESULT_OK, intent);
        }
    }

}
